#!/bin/sh

# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2016-present Team LibreELEC (https://libreelec.tv)

# the script uses 7z to extract the iso to avoid root 
# sudo apt-get install p7zip-full

# set version you want to use
if [ -z "$1" ]; then
  echo "Usage: $0 5.0.123"
  exit 0
fi

VBOX_VERSION="$1"

echo "getting sources..."
  mkdir vbox && cd vbox/
  wget http://download.virtualbox.org/virtualbox/${VBOX_VERSION}/VBoxGuestAdditions_${VBOX_VERSION}.iso

echo "extracting iso..."
  7z e VBoxGuestAdditions_${VBOX_VERSION}.iso VBoxLinuxAdditions.run -r
  chmod 0755 VBoxLinuxAdditions.run

echo "extract VBoxLinuxAdditions.run..."
  ./VBoxLinuxAdditions.run --tar xf

echo "extract VBoxGuestAdditions-amd64..."
  mkdir xf86-video-virtualbox-${VBOX_VERSION} 
  tar -jxvf VBoxGuestAdditions-amd64.tar.bz2 -C xf86-video-virtualbox-${VBOX_VERSION}/

echo "packing sources..."
  tar cvJf xf86-video-virtualbox-${VBOX_VERSION}.tar.xz xf86-video-virtualbox-${VBOX_VERSION}
  mv xf86-video-virtualbox-${VBOX_VERSION}.tar.xz ../xf86-video-virtualbox-${VBOX_VERSION}.tar.xz

echo "remove temporary sourcedir..."
  cd ..  
  rm -rf vbox/
